package com.hly.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.common.utils.PageUtils;
import com.hly.common.utils.Query;
import com.hly.product.dao.CategoryBrandRelationDao;
import com.hly.product.entity.BrandEntity;
import com.hly.product.entity.CategoryBrandRelationEntity;
import com.hly.product.entity.CategoryEntity;
import com.hly.product.service.BrandService;
import com.hly.product.service.CategoryBrandRelationService;
import com.hly.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public boolean updateBrandNameByBrandId(Long brandId, String brandName) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandName(brandName);
        return this.update(relationEntity,new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    /**
     * 根据指定的id，获取商品规格列表
     * @param brandId 商品id
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> selectRelationByBrandId(Long brandId) {
        return this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    @Transactional
    @Override
    public boolean saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        BrandEntity brandEntity = new BrandEntity();
        CategoryEntity categoryEntity = new CategoryEntity();
        if(categoryBrandRelation.getBrandId() != null){
            brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        }
        if(categoryBrandRelation.getCatelogId() != null){
            categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        }
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        return this.save(categoryBrandRelation);
    }

    @Override
    public List<CategoryBrandRelationEntity> selectBrandsListByCateLogId(Long cateLogId) {
        return this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",cateLogId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setCatelogName(name);
        this.update(categoryBrandRelationEntity , new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
    }

}